﻿using DGIS.Service.IService.IRenderService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Display;

namespace DGIS.Service.Service.RenderService
{
    public class SymbolService : ISymbolService
    {
        public ISymbol CreatePictureMarkerSymbol(string imgPath, int size)
        {
            ESRI.ArcGIS.Display.IPictureMarkerSymbol pictureMarkerSymbol = new ESRI.ArcGIS.Display.PictureMarkerSymbolClass();

            esriIPictureType picType = esriIPictureType.esriIPicturePNG;
            switch (System.IO.Path.GetExtension(imgPath).ToUpper())
            {
                case ".BMP":
                    picType = esriIPictureType.esriIPictureBitmap;
                    break;
                case ".PNG":
                    picType = esriIPictureType.esriIPicturePNG;
                    break;
                case ".GIF":
                    picType = esriIPictureType.esriIPictureGIF;
                    break;
                default:
                    picType = esriIPictureType.esriIPictureJPG;
                    break;
            }
            pictureMarkerSymbol.CreateMarkerSymbolFromFile(picType, imgPath);
            pictureMarkerSymbol.Angle = 0;
            pictureMarkerSymbol.BitmapTransparencyColor = new RgbColorClass() { Transparency = 0 };
            pictureMarkerSymbol.Size = size;
            pictureMarkerSymbol.XOffset = 0;
            pictureMarkerSymbol.YOffset = 0;

            return pictureMarkerSymbol as ISymbol;
        }

        public ISymbol CreateSimpleFillSymbol(IRgbColor fillColor, ISimpleLineSymbol lineSymbol)
        {
            ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
            simpleFillSymbol.Color = fillColor;
            simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
            simpleFillSymbol.Outline = lineSymbol;

            return simpleFillSymbol as ISymbol;
        }

        public ISymbol CreateSimpleLinelSymbol(IRgbColor lineColor, int width)
        {
            ISimpleLineSymbol simpleLineSymbol=new SimpleLineSymbolClass()
            {
                Style = esriSimpleLineStyle.esriSLSSolid,
                Color=lineColor,
                Width=width
            };

            return simpleLineSymbol as ISymbol;
        }

        public ISymbol GetSymbolFromStyleFile(string stylePath, string className, string symbolName)
        {
            IStyleGallery styleGalley = new ESRI.ArcGIS.Framework.StyleGalleryClass();
            IStyleGalleryStorage styleGalleryStorage = styleGalley as IStyleGalleryStorage;
            styleGalleryStorage.AddFile(stylePath);

            IEnumStyleGalleryItem enumStyleGalleryItem = styleGalley.get_Items(className, stylePath, "");

            ISymbol symbol = null;

            //加载Style文件中指定的Symbol
            IStyleGalleryItem styleGalleryItem = enumStyleGalleryItem.Next();
            while (styleGalleryItem!=null)
            {
                if (styleGalleryItem.Name == symbolName)
                {
                    symbol = styleGalleryItem.Item as ISymbol;
                    break;
                }

                styleGalleryItem = enumStyleGalleryItem.Next();
            }
            styleGalley = null;            
            styleGalleryStorage = null;
            styleGalleryItem = null;

            return symbol;
        }
    }
}
